package set

import (
	"fmt"
	"strings"
)

func NewHashSet[T comparable]() *HashSet[T] {
	return &HashSet[T]{list: make(map[T]int)}
}

type HashSet[T comparable] struct {
	list map[T]int
	size int
}

func (set *HashSet[T]) Size() int {
	return set.size
}

func (set *HashSet[T]) Add(element T) {
	set.list[element] = 0
	set.size++
}

func (set *HashSet[T]) Has(element T) bool {
	for k := range set.list {
		if k == element {
			return true
		}
	}
	return false
}
func (set *HashSet[T]) Remove(element T) {
	if set.Has(element) {
		delete(set.list, element)
		set.size--
	}
}
func (set *HashSet[T]) Clear() {
	set.list = make(map[T]int)
	set.size = 0
}

func (set HashSet[T]) String() string {
	ret := strings.Builder{}
	ret.WriteString("[")
	for k, _ := range set.list {
		ret.WriteString(fmt.Sprintf(" %v ", k))
	}
	ret.WriteString("]")
	return ret.String()
}
